package org.jeecg.modules.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.message.entity.MessageEmailConfig;
import org.jeecg.modules.message.service.IMessageEmailConfigService;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author by sean
 * @desc:
 * @since 2024-10-09 星期三 10:19:25
 */
@Service
@RequiredArgsConstructor
public class EmailService {

    private final IMessageEmailConfigService emailConfigService;

    private final TemplateEngine templateEngine;

    private final ResourceLoader resourceLoader;

    public JavaMailSender initMailSender(MessageEmailConfig emailConfig){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(emailConfig.getHost());
        mailSender.setPort(emailConfig.getPort());
        mailSender.setUsername(emailConfig.getUsername());
        mailSender.setPassword(emailConfig.getPassword());

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.auth", emailConfig.getUseAuth());
        props.put("mail.smtp.starttls.enable", emailConfig.getStarttlsEnabled());
        return mailSender;
    }

    public void sendSimpleMessage(String tenantId, String to, String subject, String text) {
        MessageEmailConfig emailConfig = emailConfigService.get(tenantId);
        JavaMailSender mailSender = initMailSender(emailConfig);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailConfig.getUsername());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }

    public void sendTemplateEmail(String tenantId, String templatePath, String to, String subject, JSONObject templateParam) throws Exception {
        MessageEmailConfig emailConfig = emailConfigService.get(tenantId);
        JavaMailSender mailSender = initMailSender(emailConfig);

        // Create the context for the template
        Context context = new Context();

        templateParam.forEach((k, v)->{
            context.setVariable(k, v);
        });

        // Process the template
        String process = templateEngine.process(templatePath, context);

        // Create the message
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        helper.setFrom(emailConfig.getUsername());
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(process, true);

        // Send the email
        mailSender.send(mimeMessage);
    }

}
